package com.sy.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.UserService;
import com.sy.dto.UserDTO;
import com.sy.dto.UserPageQueryDTO;
import com.sy.pojo.User;
import com.sy.result.PageResult;
import com.sy.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户管理
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/admin/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findAll")
    @ApiOperation("用户分页查询")
    public Result<PageResult> findAll(UserPageQueryDTO userPageQueryDTO) {
        log.info("用户分页查询：{}", userPageQueryDTO);
        PageResult pageResult = userService.findAll(userPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/doExistPhone")
    @ApiOperation("判断手机号是否存在")
    public Result<String> doExistPhone(String phone) {
        log.info("判断手机号是否存在的方法：{}", phone);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone);
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            return Result.success();
        } else {
            return Result.error("手机号已存在");
        }
    }

    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public Result update(@RequestBody UserDTO userDTO) {
        try {
            log.info("修改用户信息：{}", userDTO);
            User user = new User();
            BeanUtils.copyProperties(userDTO, user);
            userService.updateById(user);
        } catch (BeansException e) {
            throw new RuntimeException("修改员工失败");
        }
        return Result.success("修改员工成功");
    }

    @GetMapping("/doExistUsername")
    @ApiOperation("判断用户名是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", required = true,
                    value = "要验证的用户名", dataTypeClass = String.class)
    })
    public Result<String> doExistUsername(String username) {
        log.info("判断用户名是否存在的方法：{}", username);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            return Result.success();
        } else {
            return Result.error("用户名已存在");
        }
    }

    @PostMapping("/save")
    @ApiOperation("添加用户")
    public Result save(@RequestBody UserDTO userDTO) {
        try {
            log.info("添加用户：{}", userDTO);
            User user = new User();
            BeanUtils.copyProperties(userDTO, user);
            user.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
            user.setCreatetime(LocalDateTime.now());
            userService.save(user);
        } catch (BeansException e) {
            throw new RuntimeException("添加员工失败");
        }
        return Result.success("添加用户成功");
    }

    @PostMapping("/delete")
    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true,
                    value = "要删除的用户id集合", dataTypeClass = List.class)
    })
    public Result delete(@RequestBody List<Integer> ids) {
        try {
            log.info("删除用户：{}", ids);
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(User::getUid, ids);
            User user = new User();
            user.setIsDelete(0);
            userService.update(user, queryWrapper);
        } catch (Exception e) {
            throw new RuntimeException("用户删除失败");
        }
        return Result.success("用户删除成功");
    }
}
